Ext.define('PJ.view.setassessmentpaper.ConfigureCourseList', {
    extend: 'AM.default.DefaultList',
    alias: ['widget.configureCourseList'],
    requires: ['AM.default.DateTime', 
               "PJ.view.setassessmentpaper.CourseSelectStaffList",
               'AM.default.DefaultDateField'],
    initComponent: function(){
    	var me = this;
        me.columns = [{
        	header: '问卷名称',
            dataIndex: 'questionnaireName',
            flex: 1,
            minWidth: 70,
            sortable: false,
			renderer: function(value, metaData, record){
				metaData.tdAttr = 'data-qtip="' + value + '" classCourseId="' + record.get("classCourseId") + '"';
				return '<span style="cursor: pointer;color:blue">'+value+'</span> ';
			},
			listeners: {
	            click: function(g, clm){
	            	Ext.create('AM.default.DefaultWindowView',{
	            		title: "配置评估问卷",
	            		grid: me,
	            		height: 400,
	            		width: 900,
	            		classCourseId:clm.getAttribute("classCourseId"),
	            		items:[{
	                        xtype: 'courseSelectStaffList'
	                    }]
	            	});
	            }
			}
        },{
            header: '课程名称',
            dataIndex: 'courseName',
            flex: 1,
            minWidth: 70,
            sortable: false,
			renderer: function(value, metaData, record){
				metaData.tdAttr = 'data-qtip="' + value + '"';
				return value;
			}
        }/*,{
            header: '课程编号',
            dataIndex: 'courseNum',
            width: 110,
            sortable: false,
            renderer: function(value, metaData, record){
            	metaData.tdAttr = 'data-qtip="' + value + '"';
                return value;
            }
        }*/, {
            header: '学时',
            dataIndex: 'studyScore',
            width: 60,
            hidden:true,
            sortable: false,
            renderer: function(value, metaData, record){
                return value;
            }
        }, {
            header: '主讲教师',
            dataIndex: 'speaker',
            width: 80,
            sortable: false,
            renderer: function(value, metaData, record){
            	metaData.tdAttr = 'data-qtip="' + value + '"';
            	return value;
            }
        }, {
            header: '评估开始时间',
            dataIndex: 'assessCourseStartDateStr',
            width: 120,
            sortable: false,
            renderer: function(value, metaData, record){
            	metaData.tdAttr = 'data-qtip="' + value + '"';
            	return value;
            }
        }, {
            header: '评估结束时间',
            dataIndex: 'assessCourseEndDateStr',
            width: 120,
            sortable: false,
            renderer: function(value, metaData, record){
            	metaData.tdAttr = 'data-qtip="' + value + '"';
            	return value;
            }
        }, {
            header: '积称/职务',
            dataIndex: 'dutyRemark',
            width: 80,
            sortable: false,
            hidden:true,
            renderer: function(value, metaData, record){
            	metaData.tdAttr = 'data-qtip="' + value + '"';
            	return value;
            }
        }, {
            header: '教师单位',
            dataIndex: 'speakdept',
            hidden:true,
            width: 80,
            renderer: function(value, metaData, record){
            	metaData.tdAttr = 'data-qtip="' + value + '"';
            	return value;
            }
        }, {
            header: '授课方式',
            dataIndex: 'speakwayStr',
            width: 80,
            hidden:true,
            sortable: false,
            renderer: function(value, metaData, record){
            	metaData.tdAttr = 'data-qtip="' + value + '"';
            	return value;
            }
        }, {
            header: '课程要点',
            dataIndex: 'content',
            width: 80,
            hidden:true,
            sortable: false,
            renderer: function(value, metaData, record){
            	metaData.tdAttr = 'data-qtip="' + value + '"';
            	return value;
            }
        }];
        me.store = Ext.create('PJ.store.setassessmentpaper.ConfigureCourseList', {
            isLoad: false
        });
        this.loadType = gj_onlineLearning_constant.loadType;//加载方式
        me.callParent(arguments);
    },
  //配置问卷
    onConfigureClick: function(button){
        var me = this;
        var records = me.getSelectionModel().getSelection();
        var surveyObjectIds = new Array();
        for(var i=0;i<records.length;i++){
        	surveyObjectIds.push(records[i].get("classCourseId"));
        }
        /*for(var i=0;i<records.length;i++){
        	if(records[i].get("questionnaireState")==1){
                Ext.Msg.show({
                    title: '警告',
                    msg: '所选课程中含有已配置问卷的，请先将所配问卷删除!',
                    icon: Ext.Msg.WARNING,
                    buttons: Ext.Msg.OK
                });
                return;
            }
        }*/
        /*
        if (records.length > 1) {
            Ext.Msg.show({
                title: '警告',
                msg: '操作时只可选中一条记录！',
                icon: Ext.Msg.WARNING,
                buttons: Ext.Msg.OK
            });
            return;
        }
		*/
    	Ext.create('AM.default.DefaultWindowView',{
    		title: "配置评估问卷",
    		grid: me,
    		height: 400,
    		width: 900,
    		classCourseId:surveyObjectIds,
    		items:[{
                xtype: 'courseSelectStaffList'
            }]
    	});
    },
    /**
     * 设置评估时间
     */
    onSetEvaluationTimeClick: function(){
    	var me = this;
    	var records = me.getSelectionModel().getSelection();
    	var courseId = new Array();
    	for(var i=0;i<records.length;i++){
    		courseId.push(records[i].get("classCourseId"));
    	}
    	win = Ext.create('PJ.view.setassessmentpaper.SetEvaluationTime',{
    		title: "配置评估时间",
    		height: 120,
    		width: 300,
    		grid: me,
    		courseId:courseId,
    		classId:me.up("window").searchClassId
    	}).show();
    	var actionName = "load";//默认使用非JSONP的加载方法名“load”
        if(gj_onlineLearning_constant.loadType=="jsonp") {
        	actionName= "loadJsonp";
        }
        win.down('form').getForm().doAction(actionName,{
        	url: me.store.preUpdateUrl,
            params: {
            	classId: me.up("window").searchClassId,
            	courseIds:courseId
            },
            success:function(form,action){
            	var data = action.response.responseText;
    			var json = gj_onlineLearning_constant.loadType=="ajax" ? Ext.decode(data, true) : action.response;
    			var form = win.down('form');	 
    			var baseForm = form.getForm();
    			var assessCourseStartDay = json.data.assessCourseStartDay;
    			var assessCourseEndDay = json.data.assessCourseEndDay;
    				baseForm.findField("assessCourseStartDay").setValue(assessCourseStartDay);//结束前
    				baseForm.findField("assessCourseEndDay").setValue(assessCourseEndDay);//结束后
            }
        });
    },
    /**
     * 删除评估问卷
     */
    onRemoveQuestionnaireClick: function(){
    	var me = this;
        var records = this.getSelectionModel().getSelection();
        
        var classCourseIds = new Array();
        Ext.Array.each(records, function(name, index){
        	classCourseIds.push(name.get("classCourseId"));
        });
    	if(gj_onlineLearning_constant.loadType == "jsonp"){
    		Ext.Msg.confirm({
                title: '提示',
                msg: '您确定要删除所选课程的评估问卷吗？',
                buttons: Ext.Msg.OKCANCEL,
                fn: function(id){
                    if (id == 'ok') {
//                        me.loadMask.show();
                        var store = Ext.create('AM.default.jsonp.DefaultAjaxStore',{
            				loadType: gj_onlineLearning_constant.loadType,
            				url: me.store.deleteUrl,
            				extraParams: {
            					courseIds: classCourseIds
            				}
            			});
            			store.load(function(rec, operation, success){
//            				me.loadMask.hide();
            				if(success){
            					Orchid.alert('消息', '删除成功！', function(){
                                    var store = me.getStore();
                                    store.load();
                                });
            				}else{
            					Orchid.alert('消息', operation.error);
            				}
            			});
                    }
                    else {
                    	Ext.getCmp("loadingMask").hide();
                        return;
                    }
                }
            });
        }else if(gj_onlineLearning_constant.loadType == "ajax"){
        	Ext.Msg.confirm({
                title: '提示',
                msg: '您确定要删除所选课程的评估问卷吗？',
                buttons: Ext.Msg.OKCANCEL,
                fn: function(id){
                    if (id == 'ok') {
//                        me.loadMask.show();
                        Ext.Ajax.request({
                            url: me.store.deleteUrl,
                            params: {
                            	courseIds: classCourseIds
                            },
                            success: function(response){
//                                me.loadMask.hide();
                                var data = response.responseText;
                                var json = Ext.decode(data, true);
                                if (json.success) {
//                                    Ext.Msg.alert('消息', json.msg, function(){
                                	Orchid.alert('消息', '删除成功！', function(){
                                        var store = me.getStore();
                                        store.load();
                                    });
                                }
                                else {
                                    Ext.Msg.alert('消息', json.msg, function(){
                                        var store = me.getStore();
                                        store.load();
                                    });
                                }
                            }
                        });
                    }
                    else {
                    	Ext.getCmp("loadingMask").hide();
                        return;
                    }
                }
            });
        }
    },
    listeners: {
    	selectionchange: function(selModel, selected){
            this.down('#' + this.oldId + "Configure").setDisabled(selected.length === 0);
            this.down('#' + this.oldId + "SetEvaluationTime").setDisabled(selected.length === 0);
            this.down('#' + this.oldId + "RemoveQuestionnaire").setDisabled(selected.length === 0);
        },
    	viewready: function(){
    		var me = this;
    		this.addDocked([{
                xtype: 'toolbar',
                dock: 'top',
                displayInfo: true,
                items: [{
                    text: '配置评估问卷',
                    iconCls: 'publishIco',
                    itemId: this.oldId + "Configure",
                    disabled: true, 
                    scope: this,
                    handler: this.onConfigureClick
                },'-',{
                    text: '设置评估时间',
                    iconCls: 'publishIco',
                    itemId: this.oldId + "SetEvaluationTime",
                    disabled: true, 
                    scope: this,
                    handler: this.onSetEvaluationTimeClick
                },'-',{
                    text: '删除评估问卷',
                    iconCls: 'deleteIco',
                    itemId: this.oldId + "RemoveQuestionnaire",
                    disabled: true, 
                    scope: this,
                    handler: this.onRemoveQuestionnaireClick
                }, "->", this.countSelectPanel]
            }]);
    		var store = this.store;
    		var params = store.proxy.extraParams;
    		var newParams = {
    				searchClassId: me.up("window").searchClassId
    		};
    		Ext.apply(params, newParams);
    		store.isLoad = true;
    		store.load();
    		// 触发权限过滤事件
    		this.fireEvent("permission", this);
    	}
    }
});

